package P080_UseVectorAndHashTable;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

/**
 * @ClassName : UseVectorAndHashTable
 * @Description :多线程使用Vector或者HashTable
 * @Author : wukj1
 * @Date: 2022/4/7
 **/
public class UseVectorAndHashTable {

    @Test
    public void test(){
        //火车票列表
        final List<String> tickets = new Vector<>();
        //初始化票据池
        for (int i = 0; i < 100000; i++) {
            tickets.add("火车票" + i);
        }
        //退票
//        Thread returnThread = new Thread() {
//            @Override
//            public void run(){
//                while(true){
//                    tickets.add("车票" + new Random().nextInt());
//                }
//            }
//        };
        //售票
//        Thread saleThread = new Thread() {
//            @Override
//            public void run() {
//                for (String ticket : tickets) {
//                    tickets.remove(ticket);
//                }
//            }
//        };

//        returnThread.start();
//        saleThread.start();

        //十个窗口售票
        for (int i = 0; i < 10; i++) {
            new Thread(){
                @Override
                public void run() {
                    while(true){
                        System.out.println(Thread.currentThread().getId() + "----" + tickets.remove(0));
                    }
                }
            }.start();
        }
    }

}
